var le = document.querySelector('.le')
    var idArr = location.search.match(/id=(\d+)/)
    var tokenArr = location.search.match(/token=([^&]+)/)
    if (idArr && tokenArr) {
        var id = idArr[1]
        var token = tokenArr[1]
        document.querySelector('.list').setAttribute('href', "list.html?token=" + token + '&id=' + id)
        var xhr = new XMLHttpRequest
        xhr.open('get', `http://localhost:8888/users/info/${id}`)
        xhr.setRequestHeader('authorization', token)
        xhr.send()
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status >= 200 && xhr.status < 300) {
                    var res = xhr.responseText
                    res = JSON.parse(res)
                    var { code, info: { username } } = res
                    le.innerHTML = `
                    <span class="effort">每一个不曾起舞的日子，都是对生命的辜负</span>
                    <a data-id="${id}" data-token="${token}" href="javascript:;" class="logout">退出</a>
                `
                }
            }
        }
    }
    le.onclick = function () {
        var target = window.event.target || window.event.srcElement
        if (target.className = 'logout') {
            var xhr = new XMLHttpRequest
            xhr.open('get', 'http://localhost:8888/users/logout/' + target.getAttribute('data-id'))
            xhr.setRequestHeader('authorization', target.getAttribute('token'))
            xhr.send()
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    var res = xhr.responseText
                    res = JSON.parse(res)
                    console.log(res)
                    var { code, message } = res
                    if (code === 1) {
                        alert(message)
                        location.href = "home.html"
                    }
                }
            }
        }
    }
$li = $('nav>ul>li')
$li.mouseenter(function () {
    $(this).children('ul').stop().slideDown()
    $(this).parent().parent().next('div').stop().slideDown()
    $(this).children('a').removeClass('col_a')
})
$li.mouseleave(function () {
    $(this).children('ul').stop().slideUp()
    $(this).parent().parent().next('div').stop().slideUp()
    $(this).children('a').addClass('col_a')
})

//商品分类
var ul = document.querySelector('.category ul');
promiseAjax({
    url:"http://localhost:8888/goods/category"
}).then(res=>{
    var {list} = res;
    // 遍历创建li放在ul中
    var str = '';
    list.forEach(item=>{
        str += `
            <li>
                <a href="javascript:;">${item}</a>
            </li>
        `
    })
    ul.innerHTML = str;
})

// 定义当前页
var currentPage = 1
var pageSize = 8
var totalPage = 0
var category = '';
var search = '';
var sortMethod = 'ASC';
var sortType = 'id';
var lul = document.querySelector('.list ul');
var lol = document.querySelector('.list ol');
// 请求商品
getGoods()
function getGoods(){
    promiseAjax({
        url:"http://localhost:8888/goods/list",
        data:{
            current:currentPage,
            pagesize:pageSize,
            category,
            search,
            sortMethod,
            sortType
        }
    }).then(res=>{
        console.log(res);
        var {list,total} = res;
        var str = '';
        list.forEach(item=>{
            str += `
                <li>
                    <img src="${item.img_big_logo}" width=240 height=300>   
                    <p>${item.title}</p>
                    <span>￥${item.price}元</span> 
                    <a href="detail.html?id=${item.goods_id}&token=${token}&userid=${id}" class="btn btn-primary">查看详情</a>
                </li>
            `
        })
        lul.innerHTML = str
        // 分页
        var page = `
            <li>首页</li>
            <li>上一页</li>
        `;

        // 放页码
        totalPage = Math.ceil(total/pageSize)
        for(var i=1;i<=totalPage;i++){
            if(currentPage === i){
                page += `
                    <li style="background-color:#00f;">${i}</li>
                `
            }else{
                page += `
                    <li>${i}</li>
                `
            }
        }
        page += `
            <li>下一页</li>
            <li>尾页</li>
        `;
        lol.innerHTML = page;
    })
}
// 分页点击
lol.onclick = function(){
    var e = window.event;
    var target = e.target || e.srcElement;
    // 根据内容判断点击的是第几页
    if(target.innerText === '首页' && currentPage !== 1){
        currentPage = 1
        getGoods()
    }else if(target.innerText === '上一页' && currentPage !== 1){
        currentPage--
        getGoods()
    }else if(target.innerText === '下一页' && currentPage !== totalPage){
        currentPage++
        getGoods()
    }else if(target.innerText === '尾页' && currentPage !== totalPage){
        currentPage = totalPage
        getGoods()
    }else if(!isNaN(target.innerText) && currentPage !== target.innerText-0){
        currentPage = target.innerText-0
        getGoods()
    }
}
// 点击分类
ul.onclick = function(){
    var e = window.event;
    var target = e.target || e.srcElement;
    if(target.tagName === 'A'){
        category = target.innerText;
        getGoods()
    }
}
// 搜索
var searchBtn = document.querySelector('.search');
var keywords = document.querySelector('[name="keywords"]');
searchBtn.onclick = function(){
    search = keywords.value;
    getGoods()
}